package com.liyiqing.secondhandmall.productservice.service.impl;

import com.liyiqing.commonservice.utils.R;
import com.liyiqing.commonservice.utils.StatusCode;
import com.liyiqing.commonservice.vo.ProductDetailVo;
import com.liyiqing.commonservice.vo.ProductShowVo;
import com.liyiqing.secondhandmall.productservice.dao.ProductGroupDao;
import com.liyiqing.secondhandmall.productservice.service.ProductSpuImagesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyiqing.commonservice.utils.PageUtils;
import com.liyiqing.commonservice.utils.Query;

import com.liyiqing.secondhandmall.productservice.dao.ProductSpuInfoDao;
import com.liyiqing.secondhandmall.productservice.entity.ProductSpuInfoEntity;
import com.liyiqing.secondhandmall.productservice.service.ProductSpuInfoService;

import javax.annotation.Resource;


@Service("productSpuInfoService")
public class ProductSpuInfoServiceImpl extends ServiceImpl<ProductSpuInfoDao, ProductSpuInfoEntity> implements ProductSpuInfoService {

    @Resource
    private ProductSpuInfoDao productSpuInfoDao;


    @Autowired
    private ProductSpuInfoService productSpuInfoService;

    @Autowired
    private ProductSpuImagesService productSpuImagesService;


    @Resource
    private ProductGroupDao productGroupDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductSpuInfoEntity> page = this.page(
                new Query<ProductSpuInfoEntity>().getPage(params),
                new QueryWrapper<ProductSpuInfoEntity>()
        );

        return new PageUtils(page);
    }




    /**
     * 根据一个商品id，获取其包括所有图片以内的详细信息
     */
    @Override
    public ProductDetailVo selectProductDetailVoById(Long id) {
        //根据商品id去数据库查找对应实体
        ProductSpuInfoEntity productSpuInfo = productSpuInfoService.getById(id);
        if (productSpuInfo == null){
            return null;
        }
        //创建一个要返回给前端的对象
        ProductDetailVo productDetailVo = new ProductDetailVo();
        BeanUtils.copyProperties(productSpuInfo,productDetailVo);//属性拷贝
        //2.根据商品id 获取其所有的照片id
        List<Long> imagesId = productSpuImagesService.selectImagesIdByProductId(id);
        productDetailVo.setImagesId(imagesId);//将图片id返回给前端
        if (imagesId != null && imagesId.size()>0){
            //根据图片id，依次获取图片的url
            List<String> imagesUrl= new ArrayList<>();
            String s;
            for (int i = 0;i<imagesId.size();i++){
                s = productSpuImagesService.selectImageUrlById(imagesId.get(i));
                imagesUrl.add(s);
            }
            productDetailVo.setImagesUrl(imagesUrl);//将图片url返回给前端
        }
        return productDetailVo;
    }



    /**
     * 根据一个商品id，获取其包括一张图片的简要展示信息
     */
    @Override
    public ProductShowVo selectProductShowVoById(Long id) {
        //根据商品id去数据库查找对应实体
        ProductSpuInfoEntity productSpuInfo = productSpuInfoService.getById(id);
        if (productSpuInfo == null){
            return null;
        }
        //创建一个要返回给前端的对象
        ProductShowVo productShowVo = new ProductShowVo();
        BeanUtils.copyProperties(productSpuInfo, productShowVo);//属性拷贝
        //获取二级分类的名字
        productShowVo.setGroupName(productGroupDao.selectGroupNameById(productSpuInfo.getGroupId()));

        //2.根据商品id 获取其所有的图片id
        List<Long> imagesId = productSpuImagesService.selectImagesIdByProductId(id);
        //获取第一个图片的url
        String imageUrl = productSpuImagesService.selectImageUrlById(imagesId.get(0));
        productShowVo.setImage(imageUrl);//将图片url返回给前端
        return productShowVo;
    }


    /**
     * 工具：
     * 根据 productIds集合 内的商品id，返回每个id对应的商品简要信息
     */
    private List<ProductShowVo> getProductShowVoByIds(List<Long> productIds){
        List<ProductShowVo> productShowVoList = new ArrayList<>();
        //根据商品id获取简要的展示信息
        for (int i = 0; i < productIds.size(); i++) {
            ProductShowVo productShowVo = this.selectProductShowVoById(productIds.get(i));
            productShowVoList.add(productShowVo);
        }
        return productShowVoList;
    }



    /**
     * 根据账号id，获取该账号发布的所有商品的简要信息
     */
    @Override
    public R selectProductByMemberId(Long memberId) {
        //获取商品的主键id
        List<Long> productIds = productSpuInfoDao.selectProductIdByMemberId(memberId);
        if (productIds == null || productIds.size()==0){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        List<ProductShowVo> productShowVoList = this.getProductShowVoByIds(productIds);
        return R.ok().put("data", productShowVoList);
    }




    /**
     * 根据二级分类id，获取所有商品的简要信息
     */
    @Override
    public R selectProductByGroupId(Long groupId) {
        //获取商品的主键id
        List<Long> productIds = productSpuInfoDao.selectProductIdByGroupId(groupId);
        if (productIds == null || productIds.size()==0){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        List<ProductShowVo> productShowVoList = this.getProductShowVoByIds(productIds);
        return R.ok().put("data", productShowVoList);
    }



    /**
     * 根据搜索框的关键字，模糊查询商品
     */
    @Override
    public R selectProductLikeKeyword(String keyword) {
        //获取商品的主键id
        List<Long> productIds = productSpuInfoDao.selectProductLikeKeyword(keyword);
        if (productIds == null || productIds.size()==0){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        List<ProductShowVo> productShowVoList = this.getProductShowVoByIds(productIds);
        return R.ok().put("data", productShowVoList);
    }


    @Override
    public R selectAllProduct() {
        //获取所有商品的主键id
        List<Long> productIds = productSpuInfoDao.selectAllProductId();
        if (productIds == null || productIds.size()==0){
            return R.error(StatusCode.DATA_NOT_FOUND.getCode(),StatusCode.DATA_NOT_FOUND.getMsg());
        }
        List<ProductShowVo> productShowVoList = this.getProductShowVoByIds(productIds);
        return R.ok().put("data", productShowVoList);
    }



}